package com.ikangtai.shecare.common.db.sync;

import android.content.Context;

import com.ikangtai.shecare.common.App;
import com.ikangtai.shecare.common.db.DBManager;
import com.ikangtai.shecare.common.eventBusMsg.sync.SyncTaskItemMsg;
import com.ikangtai.shecare.common.util.AppLog;
import com.ikangtai.shecare.common.util.OKHttpUtil;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import org.json.JSONException;
import org.json.JSONObject;
import org.simple.eventbus.EventBus;

import java.io.IOException;

/**
 * 添加删除任务列表，每天要做的任务有哪些。
 */
public class SyncTaskItem {
    private Context context;
    private SyncTaskItemMsg syncTaskItemMsg;

    public SyncTaskItem(Context context, SyncTaskItemMsg syncTaskItemMsg) {
        this.context = context;
        this.syncTaskItemMsg = syncTaskItemMsg;
        EventBus.getDefault().register(this);
    }

    public void syncTaskItemWithNetwork() {
        final DBManager dbManager = new DBManager(context);

        String taskItems = dbManager.getUnSyncedTaskItems(App.userName);

        if (taskItems == null) {
            EventBus.getDefault().post(syncTaskItemMsg);
        } else {
            try {
                OKHttpUtil okHttpClient = new OKHttpUtil(context);
                String appendUrl = "care/updateTaskID.json";

                AppLog.i("syncTaskItemWithNetwork is syncing ...!");

                okHttpClient.add("authToken", App.authToken);
                okHttpClient.add("taskIDs", taskItems);

                okHttpClient.postAsync(appendUrl, okHttpClient.new BaseCallBack() {

                    @Override
                    public void onFailure(Request request, IOException e) {
                        super.onFailure(request, e);
                        EventBus.getDefault().post(syncTaskItemMsg);
                    }

                    @Override
                    public void onResponse(Response response) throws IOException {
                        int respCode = 0;

                        try {
                            JSONObject respJSONObj = new JSONObject(response.body().string());
                            respCode = respJSONObj.getInt("code");
                            if (App.HTTP_RESP_OK == respCode) {
                                dbManager.updateSyncTaskItemFlag(App.userName);
                                AppLog.i("syncTaskItemWithNetwork onResponse success!");
                            } else if (respCode == App.HTTP_RESP_TOKEN_TIME_OUT) {
                                OKHttpUtil.setNewToken();
                                syncTaskItemWithNetwork();
                            } else {
                                AppLog.i("syncTaskItemWithNetwork onResponse failed! respCode is:" + respCode);
                            }
                        } catch (JSONException e) {
                            AppLog.i("syncTaskItemWithNetwork JSONException!");
                            e.printStackTrace();
                        }

                        if (respCode != App.HTTP_RESP_TOKEN_TIME_OUT)
                            EventBus.getDefault().post(syncTaskItemMsg);
                    }
                });
            } catch (JSONException e) {
                e.printStackTrace();
                EventBus.getDefault().post(syncTaskItemMsg);
            }
        }
    }
}
